﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using frag_boardgame;

namespace frag_testproject
{
    /// <summary>
    /// This class provides utilities for the unit test classes
    /// </summary>
    public class TestUtils
    {
        /// <summary>
        /// Test serialization and deserialization of the given object
        /// This method tries to serialize & deserialize the given object,
        /// and checks if the result is equal to the original.
        /// </summary>
        /// <param name="target"></param>
        public static void TestSerialization<T>(T target)
        {
            object other;

            using (MemoryStream stream = new MemoryStream())
            {
                DataContractSerializer ser = new DataContractSerializer(typeof(T));
                ser.WriteObject(stream, target);

                // Deserialize
                stream.Seek(0, SeekOrigin.Begin);
                other = (T)ser.ReadObject(stream);
            }

            int x = target.GetHashCode();
            int y = other.GetHashCode();
            Assert.AreEqual(target, other);
        }

        public static Player CreatePlayer(int row, int col, string name, int maxHealth, int startingSpeed, int startingAccuracy)
        {
            PlayerConfiguration conf = new PlayerConfiguration() { StartingPosition = new Pos(row, col), Name = name };
            conf.SetStats(maxHealth, startingSpeed, startingAccuracy);
            return new Player(conf,1);
        }

        public static Player CreatePlayer(Square sqr, string name, int maxHealth, int startingSpeed, int startingAccuracy)
        {
            Player pl = CreatePlayer(sqr.Row, sqr.Column, name, maxHealth, startingSpeed, startingAccuracy);
            sqr.player = pl;

            return pl;
        }
    }
}
